/*
 * Copyright (c) 2010 CCX(China) Co.,Ltd. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * CCX(China) Co.,Ltd. ("Confidential Information").
 * It may not be copied or reproduced in any manner without the express 
 * written permission of CCX(China) Co.,Ltd.
 *
 * Author: zhaosj
 * Date: 2010-11-26 下午03:21:34
 */
package com.webservice;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.InitialContext;
import javax.sql.DataSource;

/**
 * @author zhaosj
 * 
 */
public class DbConnection {

	private String sConnStr = "";
	final static String sDBDriver = "oracle.jdbc.driver.OracleDriver";

	public DbConnection() {
		sConnStr = "jdbc:oracle:thin:@10.0.2.211:1521:ccxe";
	}

	public DbConnection(String ip, String serviceName) {
		sConnStr = "jdbc:oracle:thin:@" + ip + ":1521:" + serviceName;
	}

	public DbConnection(String ip, String port, String serviceName) {
		sConnStr = "jdbc:oracle:thin:@" + ip + ":" + port + ":" + serviceName;
	}

	public java.sql.Connection connectDbByThin() {
		java.sql.Connection conn = null;
		try {
			Class.forName(sDBDriver);
			conn = DriverManager.getConnection(sConnStr, "ccxe", "ccxe");
		} catch (Exception e) {
			System.out.println("ERROR:" + e.getMessage());
		}
		return conn;
	}

	public java.sql.Connection connectDbByThin(String userId, String password) {
		java.sql.Connection conn = null;
		try {
			Class.forName(sDBDriver);
			conn = DriverManager.getConnection(sConnStr, userId, password);
		} catch (Exception e) {
			System.out.println("ERROR:" + e.getMessage());
		}
		return conn;
	}

	public Connection getCon() {
		try {
			DataSource ds = null;
			InitialContext ctx = new InitialContext();
			ds = (DataSource) ctx.lookup("java:comp/env/jdbc/oradb");
			Connection conn = ds.getConnection();

			return conn;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public Connection getConnection()
	{
		try {
			Connection con = connectDbByThin(); // 开发环境
			//Connection con = getCon(); // 发布环境（使用连接池）
			
			return con;
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null ;
	}
	
	public Connection getJyCon() {
		try {
			DataSource ds = null;
			InitialContext ctx = new InitialContext();
			ds = (DataSource) ctx.lookup("java:comp/env/jdbc/orajy");
			Connection conn = ds.getConnection();

			return conn;
		} catch (Exception e) {
			e.printStackTrace();
			
		}
		
		return null;
	}

	public static void main(String[] args) throws SQLException {
		DbConnection myDB = new DbConnection();

		//System.out.println(myDB.getQuote());
	}

}
